
package com.ifcar.app.view;

import java.util.Map;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.FrameLayout;
import android.widget.Gallery;
import android.widget.TextView;

import com.ifcar.app.adapter.HeadAdvertismentAdapter;
import com.ifcar.app.rss.GetColumnsInfo;
import com.ifcar.app.util.ThemeSettingsHelper;
import com.ifcar.app.activity.R;

public class HeadAdvertismentGallery extends FrameLayout implements OnItemSelectedListener {

	/**
     * Gallery
     */
    private MyGallery mGallery;

    /**
     * 指示器
     */
    private AdvDotIndicator mIndicator;

    private HeadAdvertismentAdapter  mAdapter;

    private TextView mTitle;
    private TextView mTag;
    
    public HeadAdvertismentGallery(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public HeadAdvertismentGallery(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HeadAdvertismentGallery(Context context) {
        super(context);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mGallery = (MyGallery) findViewById(R.id.gallery);
        mGallery.setAutoRun(0);
        mGallery.setOnItemSelectedListener(this);

        mIndicator = (AdvDotIndicator) findViewById(R.id.headline_indicator);
        mTitle = (TextView) findViewById(R.id.text_title);
        mTag = (TextView) findViewById(R.id.text_tag);
    }

    public Gallery getGallery() {
        return mGallery;
    }
    
    public void setAdapter(HeadAdvertismentAdapter adapter) {
        mAdapter = adapter;
        mGallery.setAdapter(adapter);
        if (adapter != null) {
            mGallery.setSelection(mAdapter.getInitPosition(0));
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mGallery.setOnItemClickListener(listener);
    }

    @Override
    public void onItemSelected(AdapterView<?> adapter, View arg1, int position, long arg3) {
        final int count = mAdapter.getRealCount();        
        final int realPosition = mAdapter.getRealPosition(position);
        if (mIndicator.getTotalItems() != count) {
            mIndicator.setTotalItems(count);
        }
        mIndicator.setCurrentItem(realPosition);
        
        mTag.setTag(realPosition);
        
        
        Runnable r = new Runnable() {            
            @Override
            public void run() {
                updateTag(realPosition);                
            }
        };
        mGallery.setXXXRunnalbe(r);
        
    }
    
    private void updateTag(int position) {
        if (mAdapter == null || position < 0 || position > mAdapter.getRealCount()) {
            return;
        }
        Map<String, Object> map = (Map<String, Object>)mAdapter.getItem(position);
        mTitle.setText((String)map.get(GetColumnsInfo.TITLE_TAG));
        
        // 专题，图集和文章处理，其中第一个区别其他三个
        if(position == 0){
            String sdocid = (String)map.get(GetColumnsInfo.SPECIAL_ID_TAG);
            if(!TextUtils.isEmpty(sdocid)){
                mTag.setVisibility(View.VISIBLE);
                mTag.setText("专题");
                ThemeSettingsHelper.getThemeSettingsHelper(getContext()).setViewBackgroud(getContext(), mTag, R.drawable.subject_bk);
            }else{
                mTag.setVisibility(View.GONE);
            }
        }else{
            String tag = (String)map.get(GetColumnsInfo.NEWS_TAG);
            String subtitle = "";
            if(null!=map.get(GetColumnsInfo.SUBTITLE_TAG)){
                subtitle = (String)map.get(GetColumnsInfo.SUBTITLE_TAG);
            }
            
            if(!"".equals(subtitle)){ //优先考虑副标题
                mTag.setVisibility(View.VISIBLE);
                mTag.setText(subtitle);
                ThemeSettingsHelper.getThemeSettingsHelper(getContext()).setViewBackgroud(getContext(), mTag, R.drawable.dujia_bk);
            }else{
                if("special".equals(tag)){ // 专题
                    mTag.setVisibility(View.VISIBLE);
                    mTag.setText("专题");
                    ThemeSettingsHelper.getThemeSettingsHelper(getContext()).setViewBackgroud(getContext(), mTag, R.drawable.subject_bk);
                }else if("photoset".equals(tag)){ // 图集
                    mTag.setVisibility(View.VISIBLE);
                    mTag.setText("图集");
                    ThemeSettingsHelper.getThemeSettingsHelper(getContext()).setViewBackgroud(getContext(), mTag, R.drawable.dujia_bk);
                }else if("doc".equals(tag)){ // 文章
                    mTag.setVisibility(View.GONE);
                }else if("link".equals(tag)){ // 其他链接
                    mTag.setVisibility(View.GONE);
                }else{
                    mTag.setVisibility(View.GONE);
                }
            }
            
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
    }

    public void startAutoScroll(boolean autoScroll) {
        if (mGallery != null) {
            mGallery.startAutoScroll(autoScroll);
        }
    }
    
    public void setIndicator(Context context, ThemeSettingsHelper themeSettingsHelper) {
		// TODO Auto-generated method stub
    	mIndicator.setIndicatorBg(context, themeSettingsHelper);
    	
    	if (mTag.getTag() != null) {
    	    Integer position = (Integer)mTag.getTag();
    	    updateTag(position);
    	}
        themeSettingsHelper.setTextViewColor(context, mTag, R.color.list_tag_color);
	}

    

    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        super.onWindowVisibilityChanged(visibility);
        if ((visibility != VISIBLE || !isShown()) && mGallery != null && mAdapter != null) {
            if (mAdapter.getCount() > 0) {
                mGallery.setSelection(mAdapter.getInitPosition(mGallery.getSelectedItemPosition()));
            }
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mGallery != null && mAdapter != null) {
            if (mAdapter.getCount() > 0) {
                mGallery.setSelection(mAdapter.getInitPosition(mGallery.getSelectedItemPosition()));
            }
        }
    }
    
}
